package controllers

import (
	"gin-demo/models"
	"strconv"

	"github.com/gin-gonic/gin"
)

type UserData struct {
	Id             int    `json:"id"`
	UserId         int    `json:"user_id"`
	Nickname       string `json:"nickname"`
	Avatar         string `json:"avatar"`
	SexShow        string `json:"sex"`
	Introduction   string `json:"introduction"`
	Homepage       string `json:"homepage"`
	AddTimeShow    string `json:"add_time"`
	UpdateTimeShow string `json:"update_time"`
}

func GetUserList(c *gin.Context) {
	rs, err := models.GetUserList()
	if err != nil {
		ReturnError(c, 1001, "数据为空")
		return
	}

	count, _ := models.GetUserCount()
	var data []UserData
	var item UserData
	for _, v := range rs {
		item.Id = v.Id
		item.UserId = v.UserId
		item.Nickname = v.Nickname
		item.Avatar = v.Avatar
		if v.Sex == "1" {
			item.SexShow = "男"
		} else if v.Sex == "2" {
			item.SexShow = "女"
		} else {
			item.SexShow = "保密"
		}
		item.AddTimeShow = DateFormat(v.AddTime)
		item.UpdateTimeShow = DateFormat(v.UpdateTime)
		data = append(data, item)
	}

	ReturnSuccess(c, 0, "success", data, count)
	return
}

func GetUserInfo(c *gin.Context) {
	userIdStr := c.Param("user_id")
	userId, _ := strconv.Atoi(userIdStr)
	rs, err := models.GetUserInfo(userId)
	if err != nil {
		ReturnError(c, 1001, "数据为空")
		return
	}

	var data UserData
	data.Id = rs.Id
	data.UserId = rs.UserId
	data.Nickname = rs.Nickname
	data.Avatar = rs.Avatar
	if rs.Sex == "1" {
		data.SexShow = "男"
	} else if rs.Sex == "2" {
		data.SexShow = "女"
	} else {
		data.SexShow = "保密"
	}
	data.AddTimeShow = DateFormat(rs.AddTime)
	data.UpdateTimeShow = DateFormat(rs.UpdateTime)

	ReturnSuccess(c, 0, "success", data, 1)
	return
}
